package com.ruoyi.web.controller.myController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.system.domain.LiveAuthentication;
import com.ruoyi.system.domain.LiveRoom;
import com.ruoyi.system.domain.vo.Results;
import com.ruoyi.system.mapper.LiveRoomMapper;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api")
public class LiveRoomController {
    @Autowired
    private LiveRoomMapper liveRoomMapper;

    @PostMapping("/saveLiveRoom")
    public Results saveLiveRoom(@RequestBody LiveRoom liveRoom){
        LambdaQueryWrapper<LiveRoom> liveAuthenticationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        liveAuthenticationLambdaQueryWrapper.eq(LiveRoom::getUid,liveRoom.getUid());
        LiveRoom liveRooms = liveRoomMapper.selectOne(liveAuthenticationLambdaQueryWrapper);
        if(liveRooms!=null){
            return new Results(500,"新增失败",null);
        }
        int insert = liveRoomMapper.insert(liveRoom);
        return new Results(400,"新增成功",insert);
    }
    @GetMapping("/selectLiveRooms")
    public Results selectLiveRooms(){
        LambdaQueryWrapper<LiveRoom> liveAuthenticationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        liveAuthenticationLambdaQueryWrapper.eq(LiveRoom::getStatus,1);
        List<LiveRoom> liveRooms = liveRoomMapper.selectList(liveAuthenticationLambdaQueryWrapper);
        return new Results(400,"查找成功",liveRooms);
    }
    @GetMapping("/selectLiveRoom/{uid}")
    public Results selectLiveRoom(@PathVariable("uid")Integer uid){
        LambdaQueryWrapper<LiveRoom> liveAuthenticationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        liveAuthenticationLambdaQueryWrapper.eq(LiveRoom::getUid,uid);
        LiveRoom liveRoom = liveRoomMapper.selectOne(liveAuthenticationLambdaQueryWrapper);
        return new Results(400,"查找成功",liveRoom);
    }
    @GetMapping("/deleteLiveRoom/{uid}")
    public Results deleteLiveRoom(@PathVariable("uid")Integer uid){
        LambdaQueryWrapper<LiveRoom> liveAuthenticationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        liveAuthenticationLambdaQueryWrapper.eq(LiveRoom::getUid,uid);
        int i = liveRoomMapper.delete(liveAuthenticationLambdaQueryWrapper);
        return new Results(400,"删除成功",i);
    }
    @GetMapping("/updateLiveRoom/{uid}/{liveTitle}/{type}")
    public Results updateLiveRoom(@PathVariable("uid")Integer uid,@PathVariable("liveTitle") String title,@PathVariable("type")Integer type){
        LambdaQueryWrapper<LiveRoom> liveAuthenticationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        liveAuthenticationLambdaQueryWrapper.eq(LiveRoom::getUid,uid);
        LiveRoom liveRoom = liveRoomMapper.selectOne(liveAuthenticationLambdaQueryWrapper);
        liveRoom.setLiveTitle(title);
        liveRoom.setType(type);
        int update = liveRoomMapper.updateById(liveRoom);
        return new Results(400,"修改成功",update);
    }
    @GetMapping("/updateLiveRoom/{uid}")
    public Results updateLiveRoom(@PathVariable("uid")Integer uid){
        LambdaQueryWrapper<LiveRoom> liveAuthenticationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        liveAuthenticationLambdaQueryWrapper.eq(LiveRoom::getUid,uid);
        LiveRoom liveRoom = liveRoomMapper.selectOne(liveAuthenticationLambdaQueryWrapper);
        liveRoom.setStatus(1);
        liveRoom.setTime(new Date());
        int update = liveRoomMapper.updateById(liveRoom);
        return new Results(400,"修改成功",update);
    }
    @GetMapping("/closeLiveRoom/{uid}")
    public Results closeLiveRoom(@PathVariable("uid")Integer uid){
        LambdaQueryWrapper<LiveRoom> liveAuthenticationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        liveAuthenticationLambdaQueryWrapper.eq(LiveRoom::getUid,uid);
        LiveRoom liveRoom = liveRoomMapper.selectOne(liveAuthenticationLambdaQueryWrapper);
        liveRoom.setStatus(0);
        int update = liveRoomMapper.updateById(liveRoom);
        return new Results(400,"修改成功",update);
    }
}
